package com.example.barcodescanner;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SmsActivity extends Activity {

	Button send;
	EditText number;
	EditText msg;
	String msgText;
	/** This method is called at the time of the creation of activity. */
	@Override
	public void onCreate(Bundle instanceState) {
		super.onCreate(instanceState);
		setContentView(R.layout.activity_sms);

		send = (Button) findViewById(R.id.btnSendSMS);
		number = (EditText) findViewById(R.id.txtPhoneNo);
		msg = (EditText) findViewById(R.id.txtMessage);

		msgText =  getIntent().getExtras().getString("msgText");
		
		msg.setText(msgText);
		send.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String pNumber = number.getText().toString();
				String message = msg.getText().toString();
				if (message.length() > 0 && pNumber.length() > 0)
					sendSMS(pNumber, message);
				else
					Toast.makeText(getBaseContext(),
							"Enter both phone number and message.",
							Toast.LENGTH_SHORT).show();
			}
		});
	}

	/** This method is used to send sms to other device */
	@SuppressLint({ "NewApi", "NewApi", "NewApi" })
	private void sendSMS(String pNumber, String message) {
		String SENT = "SMS sent.";
		String DELIVERED = "SMS delivered.";
		Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
		PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0,
				new Intent(SENT), 0);

		PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this,
				0, new Intent(DELIVERED), 0);

		/** when the SMS has been sent */
		registerReceiver(new BroadcastReceiver() {

			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "General failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		/** The following method is called the SMS is delivered. */
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));

		SmsManager sms = SmsManager.getDefault();
		// sms.sendTextMessage(pNumber, null, message, sentPendingIntent,
		// deliveredPendingIntent);
	}
}
